home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / methods.lisp < prev    next >
Lisp/Scheme  |  1993-01-07  |  60KB  |  1,647 lines

  1. ;;;-*-Mode:LISP; Package: PCL; Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27.  
  28. (in-package :pcl)
  29.  
  30. (defmethod print-object (instance stream)
  31.   (printing-random-thing (instance stream)
  32.     (let ((name (class-name (class-of instance))))
  33.       (if name
  34.       (format stream "~S" name)
  35.       (format stream "Instance")))))
  36.  
  37. (defmethod print-object ((class class) stream)
  38.   (named-object-print-function class stream))
  39.  
  40. (defmethod print-object ((slotd slot-definition) stream)
  41.   (named-object-print-function slotd stream))
  42.  
  43. (defun named-object-print-function (instance stream
  44.                     &optional (extra nil extra-p))
  45.   (printing-random-thing (instance stream)
  46.     (if extra-p                    
  47.     (format stream "~A ~S ~:S"
  48.         (capitalize-words (class-name (class-of instance)))
  49.         (slot-value-or-default instance 'name)
  50.         extra)
  51.     (format stream "~A ~S"
  52.         (capitalize-words (class-name (class-of instance)))
  53.         (slot-value-or-default instance 'name)))))
  54.  
  55. (defmethod print-object ((mc standard-method-combination) stream)
  56.   (printing-random-thing (mc stream)
  57.     (format stream
  58.         "Method-Combination ~S ~S"
  59.         (slot-value-or-default mc 'type)
  60.         (slot-value-or-default mc 'options))))
  61.  
  62.  
  63. ;;;
  64. ;;;
  65. ;;;
  66. (defmethod shared-initialize :after ((slotd standard-slot-definition) slot-names &key)
  67.   (declare (ignore slot-names))
  68.   (with-slots (allocation class)
  69.     slotd
  70.     (setq allocation (if (eq allocation :class) class allocation))))
  71.  
  72. (defmethod shared-initialize :after ((slotd structure-slot-definition) slot-names 
  73.                      &key (allocation :instance))
  74.   (declare (ignore slot-names))
  75.   (unless (eq allocation :instance)
  76.     (error "structure slots must have :instance allocation")))
  77.  
  78. (defmethod inform-type-system-about-class ((class structure-class) (name t))
  79.   nil)
  80.  
  81. ;;;
  82. ;;; METHODS
  83. ;;;
  84. ;;; Methods themselves are simple inanimate objects.  Most properties of
  85. ;;; methods are immutable, methods cannot be reinitialized.  The following
  86. ;;; properties of methods can be changed:
  87. ;;;   METHOD-GENERIC-FUNCTION
  88. ;;;   METHOD-FUNCTION            ??
  89. ;;;   
  90. ;;;
  91.  
  92. (defmethod method-function ((method standard-method))
  93.   (or (slot-value method 'function)
  94.       (let ((fmf (slot-value method 'fast-function)))
  95.     (unless fmf ; the :before shared-initialize method prevents this
  96.       (error "~S doesn't seem to have a method-function" method))
  97.     (setf (slot-value method 'function)
  98.           (method-function-from-fast-function fmf)))))
  99.  
  100. (defmethod accessor-method-class ((method standard-accessor-method))
  101.   (car (slot-value method 'specializers)))
  102.  
  103. (defmethod accessor-method-class ((method standard-writer-method))
  104.   (cadr (slot-value method 'specializers)))
  105.  
  106. (defmethod print-object ((method standard-method) stream)
  107.   (printing-random-thing (method stream)
  108.     (if (slot-boundp method 'generic-function)
  109.     (let ((generic-function (method-generic-function method))
  110.           (class-name (capitalize-words (class-name (class-of method)))))
  111.       (format stream "~A ~S ~{~S ~}~:S"
  112.           class-name
  113.           (and generic-function (generic-function-name generic-function))
  114.           (method-qualifiers method)
  115.           (unparse-specializers method)))
  116.     (call-next-method))))
  117.  
  118. (defmethod print-object ((method standard-accessor-method) stream)
  119.   (printing-random-thing (method stream)
  120.     (if (slot-boundp method 'generic-function)
  121.     (let ((generic-function (method-generic-function method))
  122.           (class-name (capitalize-words (class-name (class-of method)))))
  123.       (format stream "~A ~S, slot:~S, ~:S"
  124.           class-name
  125.           (and generic-function (generic-function-name generic-function))
  126.           (accessor-method-slot-name method)
  127.           (unparse-specializers method)))
  128.     (call-next-method))))
  129.  
  130. ;;;
  131. ;;; INITIALIZATION
  132. ;;;
  133. ;;; Error checking is done in before methods.  Because of the simplicity of
  134. ;;; standard method objects the standard primary method can fill the slots.
  135. ;;;
  136. ;;; Methods are not reinitializable.
  137. ;;; 
  138.  
  139. (defmethod reinitialize-instance ((method standard-method) &rest initargs)
  140.   (declare (ignore initargs))
  141.   (error "Attempt to reinitialize the method ~S.~%~
  142.           Method objects cannot be reinitialized."
  143.      method))
  144.  
  145. (defmethod legal-documentation-p ((object standard-method) x)
  146.   (if (or (null x) (stringp x))
  147.       t
  148.       "a string or NULL"))
  149.  
  150. (defmethod legal-lambda-list-p ((object standard-method) x)
  151.   (declare (ignore x))
  152.   t)
  153.  
  154. (defmethod legal-method-function-p ((object standard-method) x)
  155.   (if (functionp x)
  156.       t
  157.       "a function"))
  158.  
  159. (defmethod legal-qualifiers-p ((object standard-method) x)
  160.   (flet ((improper-list ()
  161.        (return-from legal-qualifiers-p "Is not a proper list.")))
  162.     (dolist-carefully (q x improper-list)
  163.       (let ((ok (legal-qualifier-p object q)))
  164.     (unless (eq ok t)
  165.       (return-from legal-qualifiers-p
  166.         (format nil "Contains ~S which ~A" q ok)))))
  167.     t))
  168.  
  169. (defmethod legal-qualifier-p ((object standard-method) x)
  170.   (if (and x (atom x))
  171.       t
  172.       "is not a non-null atom"))
  173.  
  174. (defmethod legal-slot-name-p ((object standard-method) x)
  175.   (cond ((not (symbolp x)) "is not a symbol and so cannot be bound")
  176.     ((keywordp x)      "is a keyword and so cannot be bound")
  177.     ((memq x '(t nil)) "cannot be bound")
  178.     ((constantp x)     "is a constant and so cannot be bound")
  179.     (t t)))
  180.  
  181. (defmethod legal-specializers-p ((object standard-method) x)
  182.   (flet ((improper-list ()
  183.        (return-from legal-specializers-p "Is not a proper list.")))
  184.     (dolist-carefully (s x improper-list)
  185.       (let ((ok (legal-specializer-p object s)))
  186.     (unless (eq ok t)
  187.       (return-from legal-specializers-p
  188.         (format nil "Contains ~S which ~A" s ok)))))
  189.     t))
  190.  
  191. (defvar *allow-experimental-specializers-p* nil)
  192.  
  193. (defmethod legal-specializer-p ((object standard-method) x)
  194.   (if (if *allow-experimental-specializers-p*
  195.       (specializerp x)
  196.       (or (classp x)
  197.           (eql-specializer-p x)))
  198.       t
  199.       "is neither a class object nor an eql specializer"))
  200.  
  201. (defmethod shared-initialize :before ((method standard-method)
  202.                       slot-names
  203.                       &key qualifiers
  204.                        lambda-list
  205.                        specializers
  206.                        function
  207.                            fast-function
  208.                        documentation)
  209.   (declare (ignore slot-names))
  210.   (flet ((lose (initarg value string)
  211.        (error "When initializing the method ~S:~%~
  212.                    The ~S initialization argument was: ~S.~%~
  213.                    which ~A."
  214.           method initarg value string)))
  215.     (let ((check-qualifiers    (legal-qualifiers-p method qualifiers))
  216.       (check-lambda-list   (legal-lambda-list-p method lambda-list))
  217.       (check-specializers  (legal-specializers-p method specializers))
  218.       (check-function      (legal-method-function-p method (or function
  219.                                    fast-function)))
  220.       (check-documentation (legal-documentation-p method documentation)))
  221.       (unless (eq check-qualifiers t)
  222.     (lose :qualifiers qualifiers check-qualifiers))
  223.       (unless (eq check-lambda-list t)
  224.     (lose :lambda-list lambda-list check-lambda-list))
  225.       (unless (eq check-specializers t)
  226.     (lose :specializers specializers check-specializers))
  227.       (unless (eq check-function t)
  228.     (lose :function function check-function))
  229.       (unless (eq check-documentation t)
  230.     (lose :documentation documentation check-documentation)))))
  231.  
  232. (defmethod shared-initialize :before ((method standard-accessor-method)
  233.                       slot-names
  234.                       &key slot-name slot-definition)
  235.   (declare (ignore slot-names))
  236.   (unless slot-definition
  237.     (let ((legalp (legal-slot-name-p method slot-name)))
  238.       (unless (eq legalp t)
  239.     (error "The value of the :SLOT-NAME initarg ~A." legalp)))))
  240.  
  241. (defmethod shared-initialize :after ((method standard-method) slot-names
  242.                      &rest initargs
  243.                      &key qualifiers method-spec plist)
  244.   (declare (ignore slot-names method-spec plist))
  245.   (initialize-method-function initargs nil method)
  246.   (setf (plist-value method 'qualifiers) qualifiers)
  247.   #+ignore
  248.   (setf (slot-value method 'closure-generator) 
  249.     (method-function-closure-generator (slot-value method 'function))))
  250.  
  251. (defmethod shared-initialize :after ((method standard-accessor-method)
  252.                      slot-names
  253.                      &key)
  254.   (declare (ignore slot-names))
  255.   (with-slots (slot-name slot-definition)
  256.     method
  257.     (unless slot-definition
  258.       (let ((class (accessor-method-class method)))
  259.     (when (slot-class-p class)
  260.       (setq slot-definition (find slot-name (class-direct-slots class)
  261.                       :key #'slot-definition-name)))))
  262.     (when (and slot-definition (null slot-name))
  263.       (setq slot-name (slot-definition-name slot-definition)))))
  264.  
  265. (defmethod method-qualifiers ((method standard-method))
  266.   (plist-value method 'qualifiers))
  267.  
  268.  
  269.  
  270. (defvar *the-class-generic-function*          (find-class 'generic-function))
  271. (defvar *the-class-standard-generic-function* (find-class 'standard-generic-function))
  272.  
  273.  
  274.  
  275. (defmethod print-object ((generic-function generic-function) stream)
  276.   (named-object-print-function
  277.     generic-function
  278.     stream
  279.     (if (slot-boundp generic-function 'methods)
  280.     (list (length (generic-function-methods generic-function)))
  281.     "?")))
  282.  
  283. (defmethod shared-initialize :before
  284.        ((generic-function standard-generic-function)
  285.         slot-names
  286.         &key (name nil namep)
  287.          (lambda-list () lambda-list-p)
  288.          argument-precedence-order
  289.          declarations
  290.          documentation
  291.          (method-class nil method-class-supplied-p)
  292.          (method-combination nil method-combination-supplied-p))
  293.   (declare (ignore slot-names
  294.            declarations argument-precedence-order documentation
  295.            lambda-list lambda-list-p))
  296.  
  297.   (when namep
  298.     (set-function-name generic-function name))
  299.            
  300.   (flet ((initarg-error (initarg value string)
  301.        (error "When initializing the generic-function ~S:~%~
  302.                    The ~S initialization argument was: ~A.~%~
  303.                    It must be ~A."
  304.           generic-function initarg value string)))
  305.     (cond (method-class-supplied-p
  306.        (when (symbolp method-class)
  307.          (setq method-class (find-class method-class)))
  308.        (unless (and (classp method-class)
  309.             (*subtypep (class-eq-specializer method-class)
  310.                    *the-class-method*))
  311.          (initarg-error :method-class
  312.                 method-class
  313.                 "a subclass of the class METHOD"))
  314.        (setf (slot-value generic-function 'method-class) method-class))
  315.       ((slot-boundp generic-function 'method-class))
  316.       (t
  317.        (initarg-error :method-class
  318.               "not supplied"
  319.               "a subclass of the class METHOD")))
  320.     (cond (method-combination-supplied-p
  321.        (unless (method-combination-p method-combination)
  322.          (initarg-error :method-combination
  323.                 method-combination
  324.                 "a method combination object")))
  325.       ((slot-boundp generic-function 'method-combination))
  326.       (t
  327.        (initarg-error :method-combination
  328.               "not supplied"
  329.               "a method combination object")))))
  330.  
  331.  
  332. #||
  333. (defmethod reinitialize-instance ((generic-function standard-generic-function)
  334.                   &rest initargs
  335.                   &key name
  336.                        lambda-list
  337.                        argument-precedence-order
  338.                        declarations
  339.                        documentation
  340.                        method-class
  341.                        method-combination)
  342.   (declare (ignore documentation declarations argument-precedence-order
  343.            lambda-list name method-class method-combination))
  344.   (macrolet ((add-initarg (check name slot-name)
  345.            `(unless ,check
  346.           (push (slot-value generic-function ,slot-name) initargs)
  347.           (push ,name initargs))))
  348. ;   (add-initarg name :name 'name)
  349. ;   (add-initarg lambda-list :lambda-list 'lambda-list)
  350. ;   (add-initarg argument-precedence-order
  351. ;         :argument-precedence-order
  352. ;         'argument-precedence-order)
  353. ;   (add-initarg declarations :declarations 'declarations)
  354. ;   (add-initarg documentation :documentation 'documentation)
  355. ;   (add-initarg method-class :method-class 'method-class)
  356. ;   (add-initarg method-combination :method-combination 'method-combination)
  357.     (apply #'call-next-method generic-function initargs)))
  358. ||#
  359.  
  360.  
  361. ;;;
  362. ;;; These three are scheduled for demolition.
  363. ;;; 
  364. (defmethod remove-named-method (generic-function-name argument-specifiers
  365.                               &optional extra)
  366.   (let ((generic-function ())
  367.     (method ()))
  368.     (cond ((or (null (fboundp generic-function-name))
  369.            (not (generic-function-p
  370.               (setq generic-function
  371.                 (symbol-function generic-function-name)))))
  372.        (error "~S does not name a generic-function."
  373.           generic-function-name))
  374.       ((null (setq method (get-method generic-function
  375.                       extra
  376.                       (parse-specializers
  377.                         argument-specifiers)
  378.                       nil)))
  379.        (error "There is no method for the generic-function ~S~%~
  380.                    which matches the argument-specifiers ~S."
  381.           generic-function
  382.           argument-specifiers))
  383.       (t
  384.        (remove-method generic-function method)))))
  385.  
  386. (defun real-add-named-method (generic-function-name
  387.                   qualifiers
  388.                   specializers
  389.                   lambda-list
  390.                   &rest other-initargs)
  391.   #+copy-&rest-arg (setq other-initargs (copy-list other-initargs))
  392.   ;; What about changing the class of the generic-function if there is
  393.   ;; one.  Whose job is that anyways.  Do we need something kind of
  394.   ;; like class-for-redefinition?
  395.   (let* ((generic-function
  396.        (ensure-generic-function generic-function-name))
  397.      (specs (parse-specializers specializers))
  398. ;     (existing (get-method generic-function qualifiers specs nil))
  399.      (proto (method-prototype-for-gf generic-function-name))
  400.      (new (apply #'make-instance (class-of proto)
  401.                      :qualifiers qualifiers
  402.                      :specializers specs
  403.                      :lambda-list lambda-list
  404.                      other-initargs)))
  405. ;   (when existing (remove-method generic-function existing))
  406.     (add-method generic-function new)))
  407.  
  408.     
  409. (defun make-specializable (function-name &key (arglist nil arglistp))
  410.   (cond ((not (null arglistp)))
  411.     ((not (fboundp function-name)))
  412.     ((fboundp 'function-arglist)
  413.      ;; function-arglist exists, get the arglist from it.
  414.      (setq arglist (function-arglist function-name)))
  415.     (t
  416.      (error
  417.        "The :arglist argument to make-specializable was not supplied~%~
  418.             and there is no version of FUNCTION-ARGLIST defined for this~%~
  419.             port of Portable CommonLoops.~%~
  420.             You must either define a version of FUNCTION-ARGLIST (which~%~
  421.             should be easy), and send it off to the Portable CommonLoops~%~
  422.             people or you should call make-specializable again with the~%~
  423.             :arglist keyword to specify the arglist.")))
  424.   (let ((original (and (fboundp function-name)
  425.                (symbol-function function-name)))
  426.     (generic-function (make-instance 'standard-generic-function
  427.                      :name function-name))
  428.     (nrequireds 0))
  429.     (if (generic-function-p original)
  430.     original
  431.     (progn
  432.       (dolist (arg arglist)
  433.         (if (memq arg lambda-list-keywords)
  434.         (return)
  435.         (incf nrequireds)))
  436.       (setf (gdefinition function-name) generic-function)
  437.       (set-function-name generic-function function-name)
  438.       (when arglistp
  439.         (setf (gf-pretty-arglist generic-function) arglist))
  440.       (when original
  441.         (add-named-method function-name
  442.                   ()
  443.                   (make-list nrequireds :initial-element 't)
  444.                   arglist
  445.                   (list :function
  446.                     #'(lambda (args next-methods)
  447.                     (declare (ignore next-methods))
  448.                     (apply original args)))))
  449.       generic-function))))
  450.  
  451.  
  452.  
  453. (defun real-get-method (generic-function qualifiers specializers
  454.                      &optional (errorp t))
  455.   (let ((hit
  456.       (dolist (method (generic-function-methods generic-function))
  457.         (when (and (equal qualifiers (method-qualifiers method))
  458.                (every #'same-specializer-p specializers
  459.                   (method-specializers method)))
  460.           (return method)))))
  461.     (cond (hit hit)
  462.       ((null errorp) nil)
  463.       (t
  464.        (error "No method on ~S with qualifiers ~:S and specializers ~:S."
  465.           generic-function qualifiers specializers)))))
  466.  
  467.  
  468. ;;;
  469. ;;; Compute various information about a generic-function's arglist by looking
  470. ;;; at the argument lists of the methods.  The hair for trying not to use
  471. ;;; &rest arguments lives here.
  472. ;;;  The values returned are:
  473. ;;;    number-of-required-arguments
  474. ;;;       the number of required arguments to this generic-function's
  475. ;;;       discriminating function
  476. ;;;    &rest-argument-p
  477. ;;;       whether or not this generic-function's discriminating
  478. ;;;       function takes an &rest argument.
  479. ;;;    specialized-argument-positions
  480. ;;;       a list of the positions of the arguments this generic-function
  481. ;;;       specializes (e.g. for a classical generic-function this is the
  482. ;;;       list: (1)).
  483. ;;;
  484. (defmethod compute-discriminating-function-arglist-info
  485.        ((generic-function standard-generic-function))
  486.   ;;(declare (values number-of-required-arguments &rest-argument-p
  487.   ;;                 specialized-argument-postions))
  488.   (let ((number-required nil)
  489.         (restp nil)
  490.         (specialized-positions ())
  491.     (methods (generic-function-methods generic-function)))
  492.     (dolist (method methods)
  493.       (multiple-value-setq (number-required restp specialized-positions)
  494.         (compute-discriminating-function-arglist-info-internal
  495.       generic-function method number-required restp specialized-positions)))
  496.     (values number-required restp (sort specialized-positions #'<))))
  497.  
  498. (defun compute-discriminating-function-arglist-info-internal
  499.        (generic-function method number-of-requireds restp
  500.     specialized-argument-positions)
  501.   (declare (ignore generic-function) (type (or null fixnum) number-of-requireds))
  502.   (let ((requireds 0))
  503.     (declare (fixnum requireds))
  504.     ;; Go through this methods arguments seeing how many are required,
  505.     ;; and whether there is an &rest argument.
  506.     (dolist (arg (method-lambda-list method))
  507.       (cond ((eq arg '&aux) (return))
  508.             ((memq arg '(&optional &rest &key))
  509.              (return (setq restp t)))
  510.         ((memq arg lambda-list-keywords))
  511.             (t (incf requireds))))
  512.     ;; Now go through this method's type specifiers to see which
  513.     ;; argument positions are type specified.  Treat T specially
  514.     ;; in the usual sort of way.  For efficiency don't bother to
  515.     ;; keep specialized-argument-positions sorted, rather depend
  516.     ;; on our caller to do that.
  517.     (iterate ((type-spec (list-elements (method-specializers method)))
  518.               (pos (interval :from 0)))
  519.       (unless (eq type-spec *the-class-t*)
  520.     (pushnew pos specialized-argument-positions)))
  521.     ;; Finally merge the values for this method into the values
  522.     ;; for the exisiting methods and return them.  Note that if
  523.     ;; num-of-requireds is NIL it means this is the first method
  524.     ;; and we depend on that.
  525.     (values (min (or number-of-requireds requireds) requireds)
  526.             (or restp
  527.         (and number-of-requireds (/= number-of-requireds requireds)))
  528.             specialized-argument-positions)))
  529.  
  530. (defun make-discriminating-function-arglist (number-required-arguments restp)
  531.   (nconc (gathering ((args (collecting)))
  532.            (iterate ((i (interval :from 0 :below number-required-arguments)))
  533.              (gather (intern (format nil "Discriminating Function Arg ~D" i))
  534.              args)))
  535.          (when restp
  536.                `(&rest ,(intern "Discriminating Function &rest Arg")))))
  537.  
  538.  
  539. ;;;
  540. ;;;
  541. ;;;
  542.  
  543. (defmethod generic-function-lambda-list ((gf generic-function))
  544.   (gf-lambda-list gf))
  545.  
  546. (defmethod gf-fast-method-function-p ((gf standard-generic-function))
  547.   (gf-info-fast-mf-p (slot-value gf 'arg-info)))
  548.  
  549. (defmethod initialize-instance :after ((gf standard-generic-function)
  550.                        &key (lambda-list nil lambda-list-p)
  551.                        argument-precedence-order)
  552.   (with-slots (arg-info)
  553.     gf
  554.     (if lambda-list-p
  555.     (set-arg-info gf 
  556.               :lambda-list lambda-list
  557.               :argument-precedence-order argument-precedence-order)
  558.     (set-arg-info gf))
  559.     (when (arg-info-valid-p arg-info)
  560.       (update-dfun gf))))
  561.  
  562. (defmethod reinitialize-instance :after ((gf standard-generic-function)
  563.                      &rest args
  564.                      &key (lambda-list nil lambda-list-p)
  565.                      (argument-precedence-order 
  566.                       nil argument-precedence-order-p))
  567.   (with-slots (arg-info)
  568.     gf
  569.     (if lambda-list-p
  570.     (if argument-precedence-order-p
  571.         (set-arg-info gf 
  572.               :lambda-list lambda-list
  573.               :argument-precedence-order argument-precedence-order)
  574.         (set-arg-info gf 
  575.               :lambda-list lambda-list))
  576.     (set-arg-info gf))
  577.     (when (and (arg-info-valid-p arg-info)
  578.            args
  579.            (or lambda-list-p (cddr args)))
  580.       (update-dfun gf))))
  581.  
  582. ;;;
  583. ;;;
  584. ;;;
  585.  
  586. (proclaim '(special *lazy-dfun-compute-p*))
  587.  
  588. (defun set-methods (gf methods)
  589.   (setf (generic-function-methods gf) nil)
  590.   (loop (when (null methods) (return gf))
  591.     (real-add-method gf (pop methods) methods)))
  592.  
  593. (defun real-add-method (generic-function method &optional skip-dfun-update-p)
  594.   (if (method-generic-function method)
  595.       (error "The method ~S is already part of the generic~@
  596.               function ~S.  It can't be added to another generic~@
  597.               function until it is removed from the first one."
  598.          method (method-generic-function method))
  599.  
  600.       (let* ((name (generic-function-name generic-function))
  601.          (qualifiers   (method-qualifiers method))
  602.          (specializers (method-specializers method))
  603.          (existing (get-method generic-function qualifiers specializers nil)))
  604.     ;;
  605.     ;; If there is already a method like this one then we must
  606.     ;; get rid of it before proceeding.  Note that we call the
  607.     ;; generic function remove-method to remove it rather than
  608.     ;; doing it in some internal way.
  609.     ;; 
  610.     (when existing (remove-method generic-function existing))
  611.     ;;
  612.     (setf (method-generic-function method) generic-function)
  613.     (pushnew method (generic-function-methods generic-function))
  614.     (dolist (specializer specializers)
  615.       (add-direct-method specializer method))
  616.     (set-arg-info generic-function :new-method method)
  617.     (unless skip-dfun-update-p
  618.       (when (member name 
  619.             '(make-instance default-initargs
  620.               allocate-instance shared-initialize initialize-instance))
  621.         (update-make-instance-function-table (type-class (car specializers))))
  622.       (update-dfun generic-function))
  623.     method)))
  624.   
  625. (defun real-remove-method (generic-function method)
  626.   (if  (neq generic-function (method-generic-function method))
  627.        (error "The method ~S is attached to the generic function~@
  628.                ~S.  It can't be removed from the generic function~@
  629.                to which it is not attached."
  630.           method (method-generic-function method))
  631.        (let* ((name         (generic-function-name generic-function))
  632.           (specializers (method-specializers method))
  633.           (methods      (generic-function-methods generic-function))
  634.           (new-methods  (remove method methods)))          
  635.      (setf (method-generic-function method) nil)
  636.      (setf (generic-function-methods generic-function) new-methods)
  637.      (dolist (specializer (method-specializers method))
  638.        (remove-direct-method specializer method))
  639.      (set-arg-info generic-function)
  640.      (when (member name '(make-instance default-initargs
  641.                   allocate-instance shared-initialize initialize-instance))
  642.        (update-make-instance-function-table (type-class (car specializers))))
  643.      (update-dfun generic-function)
  644.      generic-function)))
  645.  
  646.  
  647. (defun compute-applicable-methods-function (generic-function arguments)
  648.   (values (compute-applicable-methods-using-types 
  649.        generic-function
  650.        (types-from-arguments generic-function arguments 'eql))))
  651.   
  652. (defmethod compute-applicable-methods 
  653.     ((generic-function generic-function) arguments)
  654.   (values (compute-applicable-methods-using-types 
  655.        generic-function
  656.        (types-from-arguments generic-function arguments 'eql))))
  657.  
  658. (defmethod compute-applicable-methods-using-classes 
  659.     ((generic-function generic-function) classes)
  660.   (compute-applicable-methods-using-types 
  661.    generic-function
  662.    (types-from-arguments generic-function classes 'class-eq)))
  663.  
  664. (defun proclaim-incompatible-superclasses (classes)
  665.   (setq classes (mapcar #'(lambda (class)
  666.                 (if (symbolp class)
  667.                 (find-class class)
  668.                 class))
  669.             classes))
  670.   (dolist (class classes)
  671.     (dolist (other-class classes)
  672.       (unless (eq class other-class)
  673.     (pushnew other-class (class-incompatible-superclass-list class))))))
  674.  
  675. (defun superclasses-compatible-p (class1 class2)
  676.   (let ((cpl1 (class-precedence-list class1))
  677.     (cpl2 (class-precedence-list class2)))
  678.     (dolist (sc1 cpl1 t)
  679.       (dolist (ic (class-incompatible-superclass-list sc1))
  680.     (when (memq ic cpl2)
  681.       (return-from superclasses-compatible-p nil))))))
  682.  
  683. (mapc
  684.  #'proclaim-incompatible-superclasses
  685.  '(;; superclass class
  686.    (built-in-class std-class structure-class) ; direct subclasses of pcl-class
  687.    (standard-class funcallable-standard-class)
  688.    ;; superclass metaobject
  689.    (class eql-specializer class-eq-specializer method method-combination
  690.     generic-function slot-definition)
  691.    ;; metaclass built-in-class
  692.    (number sequence character       ; direct subclasses of t, but not array
  693.     standard-object structure-object)   ;                         or symbol
  694.    (number array character symbol    ; direct subclasses of t, but not sequence
  695.     standard-object structure-object)
  696.    (complex float rational)        ; direct subclasses of number
  697.    (integer ratio)            ; direct subclasses of rational
  698.    (list vector)            ; direct subclasses of sequence
  699.    (cons null)                ; direct subclasses of list
  700.    (string bit-vector)            ; direct subclasses of vector
  701.    ))
  702.  
  703.  
  704.  
  705.  
  706. (defmethod same-specializer-p ((specl1 specializer) (specl2 specializer))
  707.   nil)
  708.  
  709. (defmethod same-specializer-p ((specl1 class) (specl2 class))
  710.   (eq specl1 specl2))
  711.  
  712. (defmethod specializer-class ((specializer class))
  713.   specializer)
  714.  
  715. (defmethod same-specializer-p ((specl1 class-eq-specializer)
  716.                    (specl2 class-eq-specializer))
  717.   (eq (specializer-class specl1) (specializer-class specl2)))
  718.  
  719. (defmethod same-specializer-p ((specl1 eql-specializer)
  720.                    (specl2 eql-specializer))
  721.   (eq (specializer-object specl1) (specializer-object specl2)))
  722.  
  723. (defmethod specializer-class ((specializer eql-specializer))
  724.   (class-of (slot-value specializer 'object)))
  725.  
  726. (defvar *in-gf-arg-info-p* nil)
  727. (setf (gdefinition 'arg-info-reader)
  728.       (let ((mf (initialize-method-function
  729.          (make-internal-reader-method-function
  730.           'standard-generic-function 'arg-info)
  731.          t)))
  732.     #'(lambda (&rest args) (funcall mf args nil))))
  733.  
  734. (defun types-from-arguments (generic-function arguments &optional type-modifier)
  735.   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
  736.       (get-generic-function-info generic-function)
  737.     (declare (ignore applyp metatypes nkeys))
  738.     (let ((types-rev nil))
  739.       (dotimes (i nreq)
  740.     i
  741.     (unless arguments
  742.       (error "The function ~S requires at least ~D arguments"
  743.          (generic-function-name generic-function)
  744.          nreq))
  745.     (let ((arg (pop arguments)))
  746.       (push (if type-modifier `(,type-modifier ,arg) arg) types-rev)))
  747.       (values (nreverse types-rev) arg-info))))
  748.  
  749. (defun get-wrappers-from-classes (nkeys wrappers classes metatypes)
  750.   (let* ((w wrappers) (w-tail w) (mt-tail metatypes))
  751.     (dolist (class (if (listp classes) classes (list classes)))
  752.       (unless (eq 't (car mt-tail))
  753.     (let ((c-w (class-wrapper class)))
  754.       (unless c-w (return-from get-wrappers-from-classes nil))
  755.       (if (eql nkeys 1)
  756.           (setq w c-w)
  757.           (setf (car w-tail) c-w
  758.             w-tail (cdr w-tail)))))
  759.       (setq mt-tail (cdr mt-tail)))
  760.     w))
  761.  
  762. (defun sdfun-for-caching (gf classes)
  763.   (let ((types (mapcar #'class-eq-type classes)))
  764.     (multiple-value-bind (methods all-applicable-and-sorted-p)
  765.     (compute-applicable-methods-using-types gf types)
  766.       (function-funcall (get-secondary-dispatch-function1 
  767.              gf methods types nil t all-applicable-and-sorted-p)
  768.             nil (mapcar #'class-wrapper classes)))))
  769.  
  770. (defun value-for-caching (gf classes)
  771.   (let ((methods (compute-applicable-methods-using-types 
  772.            gf (mapcar #'class-eq-type classes))))
  773.     (method-function-get (or (method-fast-function (car methods))
  774.                  (method-function (car methods)))
  775.              :constant-value)))
  776.  
  777. (defun default-secondary-dispatch-function (generic-function)
  778.   #'(lambda (&rest args)
  779.       #+copy-&rest-arg (setq args (copy-list args))
  780.       (let ((methods (compute-applicable-methods generic-function args)))
  781.     (if methods
  782.         (let ((emf (get-effective-method-function generic-function methods)))
  783.           (invoke-emf emf args))
  784.         (apply #'no-applicable-method generic-function args)))))
  785.  
  786. (defun list-eq (x y)
  787.   (loop (when (atom x) (return (eq x y)))
  788.     (when (atom y) (return nil))
  789.     (unless (eq (car x) (car y)) (return nil))
  790.     (setq x (cdr x)  y (cdr y))))
  791.  
  792. (defvar *std-cam-methods* nil)
  793.  
  794. (defun compute-applicable-methods-emf (generic-function)  
  795.   (if (eq *boot-state* 'complete)
  796.       (let* ((cam (gdefinition 'compute-applicable-methods))
  797.          (cam-methods (compute-applicable-methods-using-types
  798.                cam (list `(eql ,generic-function) t))))
  799.     (values (get-effective-method-function cam cam-methods)
  800.         (list-eq cam-methods 
  801.              (or *std-cam-methods*
  802.                  (setq *std-cam-methods*
  803.                    (compute-applicable-methods-using-types
  804.                     cam (list `(eql ,cam) t)))))))
  805.       (values #'compute-applicable-methods-function t)))
  806.  
  807. (defun compute-applicable-methods-emf-std-p (gf)
  808.   (gf-info-c-a-m-emf-std-p (gf-arg-info gf)))
  809.  
  810. (defvar *old-c-a-m-gf-methods* nil)
  811.  
  812. (defun update-all-c-a-m-gf-info (c-a-m-gf)
  813.   (let ((methods (generic-function-methods c-a-m-gf)))
  814.     (if (and *old-c-a-m-gf-methods*
  815.          (every #'(lambda (old-method)
  816.             (member old-method methods))
  817.             *old-c-a-m-gf-methods*))
  818.     (let ((gfs-to-do nil)
  819.           (gf-classes-to-do nil))
  820.       (dolist (method methods)
  821.         (unless (member method *old-c-a-m-gf-methods*)
  822.           (let ((specl (car (method-specializers method))))
  823.         (if (eql-specializer-p specl)
  824.             (pushnew (specializer-object specl) gfs-to-do)
  825.             (pushnew (specializer-class specl) gf-classes-to-do)))))
  826.       (map-all-generic-functions 
  827.        #'(lambda (gf)
  828.            (when (or (member gf gfs-to-do)
  829.              (dolist (class gf-classes-to-do nil)
  830.                (member class (class-precedence-list (class-of gf)))))
  831.          (update-c-a-m-gf-info gf)))))
  832.     (map-all-generic-functions #'update-c-a-m-gf-info))
  833.     (setq *old-c-a-m-gf-methods* methods)))
  834.  
  835. (defun update-gf-info (gf)
  836.   (update-c-a-m-gf-info gf)
  837.   (update-gf-simple-accessor-type gf))
  838.  
  839. (defun update-c-a-m-gf-info (gf)
  840.   (unless (early-gf-p gf)
  841.     (multiple-value-bind (c-a-m-emf std-p)
  842.     (compute-applicable-methods-emf gf)
  843.       (let ((arg-info (gf-arg-info gf)))
  844.     (setf (gf-info-static-c-a-m-emf arg-info) c-a-m-emf)
  845.     (setf (gf-info-c-a-m-emf-std-p arg-info) std-p)))))
  846.  
  847. (defun update-gf-simple-accessor-type (gf)
  848.   (let ((arg-info (gf-arg-info gf)))
  849.     (setf (gf-info-simple-accessor-type arg-info)
  850.       (let* ((methods (generic-function-methods gf))
  851.          (class (and methods (class-of (car methods))))
  852.          (type (and class (cond ((eq class *the-class-standard-reader-method*)
  853.                      'reader)
  854.                     ((eq class *the-class-standard-writer-method*)
  855.                      'writer)
  856.                     ((eq class *the-class-standard-boundp-method*)
  857.                      'boundp)))))
  858.         (when (and (gf-info-c-a-m-emf-std-p arg-info)
  859.                type
  860.                (dolist (method (cdr methods) t)
  861.              (unless (eq class (class-of method)) (return nil)))
  862.                (eq (generic-function-method-combination gf)
  863.                *standard-method-combination*))
  864.           type)))))
  865.  
  866. (defun get-accessor-method-function (gf type class slotd)  
  867.   (let* ((std-method (standard-svuc-method type))
  868.      (str-method (structure-svuc-method type))
  869.      (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
  870.      (types (if (eq type 'writer) `(t ,@types1) types1))
  871.      (methods (compute-applicable-methods-using-types gf types))
  872.      (std-p (null (cdr methods))))
  873.     (values
  874.      (if std-p
  875.      (get-optimized-std-accessor-method-function class slotd type)
  876.      (get-accessor-from-svuc-method-function
  877.       class slotd
  878.       (get-secondary-dispatch-function 
  879.        gf methods types
  880.        `((,(car (or (member std-method methods)
  881.             (member str-method methods)
  882.             (error "error in get-accessor-method-function")))
  883.           ,(get-optimized-std-slot-value-using-class-method-function
  884.         class slotd type)))
  885.        (unless (and (eq type 'writer)
  886.             (dolist (method methods t)
  887.               (unless (eq (car (method-specializers method))
  888.                       *the-class-t*)
  889.                 (return nil))))
  890.          (let ((wrappers (list (wrapper-of class)
  891.                    (class-wrapper class)
  892.                    (wrapper-of slotd))))
  893.            (if (eq type 'writer)
  894.            (cons (class-wrapper *the-class-t*) wrappers)
  895.            wrappers))))
  896.       type))
  897.      std-p)))
  898.  
  899. ;used by optimize-slot-value-by-class-p (vector.lisp)
  900. (defun update-slot-value-gf-info (gf type)
  901.   (unless *new-class*
  902.     (update-std-or-str-methods gf type))
  903.   (when (and (standard-svuc-method type) (structure-svuc-method type))
  904.     (flet ((update-class (class)
  905.          (when (class-finalized-p class)
  906.            (dolist (slotd (class-slots class))
  907.          (compute-slot-accessor-info slotd type gf)))))
  908.       (if *new-class*
  909.       (update-class *new-class*)
  910.       (map-all-classes #'update-class 'slot-object)))))
  911.  
  912. (defvar *standard-slot-value-using-class-method* nil)
  913. (defvar *standard-setf-slot-value-using-class-method* nil)
  914. (defvar *standard-slot-boundp-using-class-method* nil)
  915. (defvar *structure-slot-value-using-class-method* nil)
  916. (defvar *structure-setf-slot-value-using-class-method* nil)
  917. (defvar *structure-slot-boundp-using-class-method* nil)
  918.  
  919. (defun standard-svuc-method (type)
  920.   (case type
  921.     (reader *standard-slot-value-using-class-method*)
  922.     (writer *standard-setf-slot-value-using-class-method*)
  923.     (boundp *standard-slot-boundp-using-class-method*)))
  924.  
  925. (defun set-standard-svuc-method (type method)
  926.   (case type
  927.     (reader (setq *standard-slot-value-using-class-method* method))
  928.     (writer (setq *standard-setf-slot-value-using-class-method* method))
  929.     (boundp (setq *standard-slot-boundp-using-class-method* method))))
  930.  
  931. (defun structure-svuc-method (type)
  932.   (case type
  933.     (reader *structure-slot-value-using-class-method*)
  934.     (writer *structure-setf-slot-value-using-class-method*)
  935.     (boundp *structure-slot-boundp-using-class-method*)))
  936.  
  937. (defun set-structure-svuc-method (type method)
  938.   (case type
  939.     (reader (setq *structure-slot-value-using-class-method* method))
  940.     (writer (setq *structure-setf-slot-value-using-class-method* method))
  941.     (boundp (setq *structure-slot-boundp-using-class-method* method))))
  942.  
  943. (defun update-std-or-str-methods (gf type)
  944.   (dolist (method (generic-function-methods gf))
  945.     (let ((specls (method-specializers method)))
  946.       (when (and (or (not (eq type 'writer))
  947.              (eq (pop specls) *the-class-t*))
  948.          (every #'classp specls))
  949.     (cond ((and (eq (class-name (car specls))
  950.             'std-class)
  951.             (eq (class-name (cadr specls)) 
  952.             'standard-object)
  953.             (eq (class-name (caddr specls)) 
  954.             'standard-effective-slot-definition))
  955.            (set-standard-svuc-method type method))
  956.           ((and (eq (class-name (car specls))
  957.             'structure-class)
  958.             (eq (class-name (cadr specls))
  959.             'structure-object)
  960.             (eq (class-name (caddr specls)) 
  961.             'structure-effective-slot-definition))
  962.            (set-structure-svuc-method type method)))))))
  963.  
  964. (defun mec-all-classes-internal (spec precompute-p)
  965.   (cons (specializer-class spec)
  966.     (and (classp spec)
  967.          precompute-p
  968.          (not (or (eq spec *the-class-t*)
  969.               (eq spec *the-class-slot-object*)
  970.               (eq spec *the-class-standard-object*)
  971.               (eq spec *the-class-structure-object*)))
  972.          (let ((sc (class-direct-subclasses spec)))
  973.            (when sc
  974.          (mapcan #'(lambda (class)
  975.                  (mec-all-classes-internal class precompute-p))
  976.              sc))))))
  977.  
  978. (defun mec-all-classes (spec precompute-p)
  979.   (let ((classes (mec-all-classes-internal spec precompute-p)))
  980.     (if (null (cdr classes))
  981.     classes
  982.     (let* ((a-classes (cons nil classes))
  983.            (tail classes))
  984.       (loop (when (null (cdr tail))
  985.           (return (cdr a-classes)))
  986.         (let ((class (cadr tail))
  987.               (ttail (cddr tail)))
  988.           (if (dolist (c ttail nil)
  989.             (when (eq class c) (return t)))
  990.               (setf (cdr tail) (cddr tail))
  991.               (setf tail (cdr tail)))))))))
  992.  
  993. (defun mec-all-class-lists (spec-list precompute-p)
  994.   (if (null spec-list)
  995.       (list nil)
  996.       (let* ((car-all-classes (mec-all-classes (car spec-list) precompute-p))
  997.          (all-class-lists (mec-all-class-lists (cdr spec-list) precompute-p)))
  998.     (mapcan #'(lambda (list)
  999.             (mapcar #'(lambda (c) (cons c list)) car-all-classes))
  1000.         all-class-lists))))
  1001.  
  1002. (defun make-emf-cache (generic-function valuep cache classes-list new-class)
  1003.   (let* ((arg-info (gf-arg-info generic-function))
  1004.      (nkeys (arg-info-nkeys arg-info))
  1005.      (metatypes (arg-info-metatypes arg-info))
  1006.      (wrappers (unless (eq nkeys 1) (make-list nkeys)))
  1007.      (precompute-p (gf-precompute-dfun-and-emf-p arg-info))
  1008.      (default '(default)))
  1009.     (flet ((add-class-list (classes)
  1010.          (when (or (null new-class) (memq new-class classes))
  1011.            (let ((wrappers (get-wrappers-from-classes 
  1012.                 nkeys wrappers classes metatypes)))
  1013.          (when (and wrappers
  1014.                 (eq default (probe-cache cache wrappers default)))
  1015.            (let ((value (cond ((eq valuep t)
  1016.                        (sdfun-for-caching generic-function classes))
  1017.                       ((eq valuep :constant-value)
  1018.                        (value-for-caching generic-function classes)))))
  1019.              (setq cache (fill-cache cache wrappers value t))))))))
  1020.       (if classes-list
  1021.       (mapc #'add-class-list classes-list)
  1022.       (dolist (method (generic-function-methods generic-function))
  1023.         (mapc #'add-class-list
  1024.           (mec-all-class-lists (method-specializers method) precompute-p))))
  1025.       cache)))
  1026.  
  1027. (defmacro class-test (arg class)
  1028.   (cond ((eq class *the-class-t*)
  1029.      't)
  1030.     ((eq class *the-class-slot-object*)
  1031.      #+new-kcl-wrapper
  1032.      `(or (std-instance-p ,arg)
  1033.           (fsc-instance-p ,arg))
  1034.      #-new-kcl-wrapper
  1035.      `(not (eq *the-class-built-in-class* 
  1036.            (wrapper-class (std-instance-wrapper (class-of ,arg))))))
  1037.     #-new-kcl-wrapper
  1038.     ((eq class *the-class-standard-object*)
  1039.      `(or (std-instance-p ,arg) (fsc-instance-p ,arg)))
  1040.     ((eq class *the-class-structure-object*)
  1041.      `(memq ',class (class-precedence-list (class-of ,arg))))
  1042.     ;; TYPEP is now sometimes faster than doing memq of the cpl
  1043.     (t
  1044.      `(typep ,arg ',(class-name class)))))
  1045.  
  1046. (defmacro class-eq-test (arg class)
  1047.   `(eq (class-of ,arg) ',class))
  1048.  
  1049. (defmacro eql-test (arg object)
  1050.   `(eql ,arg ',object))
  1051.  
  1052. (defun dnet-methods-p (form)
  1053.   (and (consp form)
  1054.        (or (eq (car form) 'methods)
  1055.        (eq (car form) 'unordered-methods))))
  1056.  
  1057. (defmacro scase (arg &rest clauses) ; This is case, but without gensyms
  1058.   `(let ((.case-arg. ,arg))
  1059.      (cond ,@(mapcar #'(lambda (clause)
  1060.              (list* (cond ((null (car clause))
  1061.                        nil)
  1062.                       ((consp (car clause))
  1063.                        (if (null (cdar clause))
  1064.                        `(eql .case-arg. ',(caar clause))
  1065.                        `(member .case-arg. ',(car clause))))
  1066.                       ((member (car clause) '(t otherwise))
  1067.                        `t)
  1068.                       (t
  1069.                        `(eql .case-arg. ',(car clause))))
  1070.                 nil
  1071.                 (cdr clause)))
  1072.              clauses))))
  1073.  
  1074. (defmacro mcase (arg &rest clauses) `(scase ,arg ,@clauses))
  1075.  
  1076. (defun generate-discrimination-net (generic-function methods types sorted-p)
  1077.   (let* ((arg-info (gf-arg-info generic-function))
  1078.      (precedence (arg-info-precedence arg-info)))
  1079.     (generate-discrimination-net-internal 
  1080.      generic-function methods types
  1081.      #'(lambda (methods known-types)
  1082.      (if (or sorted-p
  1083.          (block one-order-p
  1084.            (let ((sorted-methods nil))
  1085.              (map-all-orders 
  1086.               (copy-list methods) precedence
  1087.               #'(lambda (methods)
  1088.               (when sorted-methods (return-from one-order-p nil))
  1089.               (setq sorted-methods methods)))
  1090.              (setq methods sorted-methods))
  1091.            t))
  1092.          `(methods ,methods ,known-types)
  1093.          `(unordered-methods ,methods ,known-types)))
  1094.      #'(lambda (position type true-value false-value)
  1095.      (let ((arg (dfun-arg-symbol position)))
  1096.        (if (eq (car type) 'eql)
  1097.            (let* ((false-case-p (and (consp false-value)
  1098.                      (or (eq (car false-value) 'scase)
  1099.                          (eq (car false-value) 'mcase))
  1100.                      (eq arg (cadr false-value))))
  1101.               (false-clauses (if false-case-p
  1102.                      (cddr false-value)
  1103.                      `((t ,false-value))))
  1104.               (case-sym (if (and (dnet-methods-p true-value)
  1105.                      (if false-case-p
  1106.                          (eq (car false-value) 'mcase)
  1107.                          (dnet-methods-p false-value)))
  1108.                     'mcase
  1109.                     'scase))
  1110.               (type-sym `(,(cadr type))))
  1111.          `(,case-sym ,arg
  1112.             (,type-sym ,true-value)
  1113.             ,@false-clauses))
  1114.            `(if ,(let ((arg (dfun-arg-symbol position)))
  1115.                (case (car type)
  1116.              (class    `(class-test    ,arg ,(cadr type)))
  1117.              (class-eq `(class-eq-test ,arg ,(cadr type)))))
  1118.             ,true-value
  1119.             ,false-value))))
  1120.      #'identity)))
  1121.  
  1122. (defun class-from-type (type)
  1123.   (if (or (atom type) (eq (car type) 't))
  1124.       *the-class-t*
  1125.       (case (car type)
  1126.     (and (dolist (type (cdr type) *the-class-t*)
  1127.            (when (and (consp type) (not (eq (car type) 'not)))
  1128.          (return (class-from-type type)))))
  1129.     (not *the-class-t*)
  1130.         (eql (class-of (cadr type)))
  1131.         (class-eq (cadr type))
  1132.         (class (cadr type)))))
  1133.  
  1134. (defun precompute-effective-methods (gf caching-p &optional classes-list-p)
  1135.   (let* ((arg-info (gf-arg-info gf))
  1136.      (methods (generic-function-methods gf))
  1137.      (precedence (arg-info-precedence arg-info))
  1138.      (*in-precompute-effective-methods-p* t)
  1139.      (classes-list nil))
  1140.     (generate-discrimination-net-internal 
  1141.      gf methods nil
  1142.      #'(lambda (methods known-types)
  1143.      (when methods
  1144.        (when classes-list-p
  1145.          (push (mapcar #'class-from-type known-types) classes-list))
  1146.        (let ((no-eql-specls-p (not (methods-contain-eql-specializer-p methods))))
  1147.          (map-all-orders 
  1148.           methods precedence
  1149.           #'(lambda (methods)
  1150.           (get-secondary-dispatch-function1 
  1151.            gf methods known-types
  1152.            nil caching-p no-eql-specls-p))))))
  1153.      #'(lambda (position type true-value false-value)
  1154.      (declare (ignore position type true-value false-value))
  1155.      nil)
  1156.      #'(lambda (type)
  1157.      (if (and (consp type) (eq (car type) 'eql))
  1158.          `(class-eq ,(class-of (cadr type)))
  1159.          type)))
  1160.     classes-list))
  1161.  
  1162. ; we know that known-type implies neither new-type nor `(not ,new-type) 
  1163. (defun augment-type (new-type known-type)
  1164.   (if (or (eq known-type 't)
  1165.       (eq (car new-type) 'eql))
  1166.       new-type
  1167.       (let ((so-far (if (and (consp known-type) (eq (car known-type) 'and))
  1168.             (cdr known-type)
  1169.             (list known-type))))
  1170.     (unless (eq (car new-type) 'not)
  1171.       (setq so-far
  1172.         (mapcan #'(lambda (type)
  1173.                 (unless (*subtypep new-type type)
  1174.                   (list type)))
  1175.             so-far)))
  1176.     (if (null so-far)
  1177.         new-type
  1178.         `(and ,new-type ,@so-far)))))
  1179.  
  1180. #+lcl3.0 (dont-use-production-compiler)
  1181.  
  1182. (defun generate-discrimination-net-internal 
  1183.     (gf methods types methods-function test-function type-function)
  1184.   (let* ((arg-info (gf-arg-info gf))
  1185.      (precedence (arg-info-precedence arg-info))
  1186.      (nreq (arg-info-number-required arg-info))
  1187.      (metatypes (arg-info-metatypes arg-info)))
  1188.     (labels ((do-column (p-tail contenders known-types)
  1189.            (if p-tail
  1190.            (let* ((position (car p-tail))
  1191.               (known-type (or (nth position types) t)))
  1192.              (if (eq (nth position metatypes) 't)
  1193.              (do-column (cdr p-tail) contenders
  1194.                     (cons (cons position known-type) known-types))
  1195.              (do-methods p-tail contenders 
  1196.                      known-type () known-types)))
  1197.            (funcall methods-function contenders 
  1198.                 (let ((k-t (make-list nreq)))
  1199.                   (dolist (index+type known-types)
  1200.                 (setf (nth (car index+type) k-t) (cdr index+type)))
  1201.                   k-t))))
  1202.          (do-methods (p-tail contenders known-type winners known-types)
  1203.            ;;
  1204.                ;; <contenders>
  1205.            ;;   is a (sorted) list of methods that must be discriminated
  1206.                ;; <known-type>
  1207.            ;;   is the type of this argument, constructed from tests already made.
  1208.                ;; <winners>
  1209.            ;;   is a (sorted) list of methods that are potentially applicable
  1210.            ;;   after the discrimination has been made.
  1211.            ;;   
  1212.                (if (null contenders)
  1213.            (do-column (cdr p-tail) winners
  1214.                   (cons (cons (car p-tail) known-type) known-types))
  1215.                    (let* ((position (car p-tail))
  1216.               (method (car contenders))
  1217.               (specl (nth position (method-specializers method)))
  1218.                           (type (funcall type-function (type-from-specializer specl))))
  1219.              (multiple-value-bind (app-p maybe-app-p)
  1220.              (specializer-applicable-using-type-p type known-type)
  1221.                (flet ((determined-to-be (truth-value)
  1222.                 (if truth-value app-p (not maybe-app-p)))
  1223.                   (do-if (truth &optional implied)
  1224.                 (let ((ntype (if truth type `(not ,type))))
  1225.                   (do-methods p-tail
  1226.                     (cdr contenders)
  1227.                     (if implied
  1228.                     known-type
  1229.                     (augment-type ntype known-type))
  1230.                     (if truth
  1231.                     (append winners `(,method))
  1232.                     winners)
  1233.                     known-types))))
  1234.              (cond ((determined-to-be nil) (do-if nil t))
  1235.                    ((determined-to-be t)   (do-if t   t))
  1236.                    (t (funcall test-function position type 
  1237.                        (do-if t) (do-if nil))))))))))
  1238.       (do-column precedence methods ()))))
  1239.  
  1240. #+lcl3.0 (use-previous-compiler)
  1241.  
  1242. (defun compute-secondary-dispatch-function (generic-function net &optional 
  1243.                         method-alist wrappers)
  1244.   (function-funcall (compute-secondary-dispatch-function1 generic-function net)
  1245.             method-alist wrappers))
  1246.  
  1247. (defvar *eq-case-table-limit* 15)
  1248. (defvar *case-table-limit* 10)
  1249.  
  1250. (defun compute-mcase-parameters (case-list)
  1251.   (unless (eq 't (caar (last case-list)))
  1252.     (error "The key for the last case arg to mcase was not T"))
  1253.   (let* ((eq-p (dolist (case case-list t)
  1254.          (unless (or (eq (car case) 't)
  1255.                  (symbolp (caar case)))
  1256.            (return nil))))
  1257.      (len (1- (length case-list)))
  1258.      (type (cond ((= len 1)
  1259.               :simple)
  1260.              ((<= len
  1261.               (if eq-p
  1262.                   *eq-case-table-limit*
  1263.                   *case-table-limit*))
  1264.               :assoc)
  1265.              (t
  1266.               :hash-table))))
  1267.     (list eq-p type)))
  1268.  
  1269. (defmacro mlookup (key info default &optional eq-p type)
  1270.   (unless (or (eq eq-p 't) (null eq-p))
  1271.     (error "Invalid eq-p argument"))
  1272.   (ecase type
  1273.     (:simple
  1274.      `(if (,(if eq-p 'eq 'eql) ,key (car ,info))
  1275.           (cdr ,info)
  1276.           ,default))
  1277.     (:assoc
  1278.      `(dolist (e ,info ,default)
  1279.         (when (,(if eq-p 'eq 'eql) (car e) ,key)
  1280.       (return (cdr e)))))
  1281.     (:hash-table
  1282.      `(gethash ,key ,info ,default))))
  1283.  
  1284. (defun net-test-converter (form)
  1285.   (if (atom form)
  1286.       (default-test-converter form)
  1287.       (case (car form)
  1288.     ((invoke-effective-method-function invoke-fast-method-call)
  1289.      '.call.)
  1290.     (methods
  1291.      '.methods.)
  1292.     (unordered-methods
  1293.      '.umethods.)
  1294.     (mcase
  1295.      `(mlookup ,(cadr form) nil nil ,@(compute-mcase-parameters (cddr form))))
  1296.     (t (default-test-converter form)))))
  1297.  
  1298. (defun net-code-converter (form)
  1299.   (if (atom form)
  1300.       (default-code-converter form)
  1301.       (case (car form)
  1302.     ((methods unordered-methods)
  1303.      (let ((gensym (gensym)))
  1304.        (values gensym
  1305.            (list gensym))))
  1306.     (mcase
  1307.      (let ((mp (compute-mcase-parameters (cddr form)))
  1308.            (gensym (gensym)) (default (gensym)))
  1309.        (values `(mlookup ,(cadr form) ,gensym ,default ,@mp)
  1310.            (list gensym default))))
  1311.     (t
  1312.      (default-code-converter form)))))
  1313.  
  1314. (defun net-constant-converter (form generic-function)
  1315.   (or (let ((c (methods-converter form generic-function)))
  1316.     (when c (list c)))
  1317.       (if (atom form)
  1318.       (default-constant-converter form)
  1319.       (case (car form)
  1320.         (mcase
  1321.          (let* ((mp (compute-mcase-parameters (cddr form)))
  1322.             (list (mapcar #'(lambda (clause)
  1323.                       (let ((key (car clause))
  1324.                         (meth (cadr clause)))
  1325.                     (cons (if (consp key) (car key) key)
  1326.                           (methods-converter
  1327.                            meth generic-function))))
  1328.                   (cddr form)))
  1329.             (default (car (last list))))
  1330.            (list (list* ':mcase mp (nbutlast list))
  1331.              (cdr default))))
  1332.         (t
  1333.          (default-constant-converter form))))))
  1334.  
  1335. (defun methods-converter (form generic-function)
  1336.   (cond ((and (consp form) (eq (car form) 'methods))
  1337.      (cons '.methods.
  1338.            (get-effective-method-function1 generic-function (cadr form))))
  1339.     ((and (consp form) (eq (car form) 'unordered-methods))
  1340.      (default-secondary-dispatch-function generic-function))))
  1341.  
  1342. (defun convert-methods (constant method-alist wrappers)
  1343.   (if (and (consp constant)
  1344.        (eq (car constant) '.methods.))
  1345.       (funcall (cdr constant) method-alist wrappers)
  1346.       constant))
  1347.  
  1348. (defun convert-table (constant method-alist wrappers)
  1349.   (cond ((and (consp constant)
  1350.           (eq (car constant) ':mcase))
  1351.      (let ((alist (mapcar #'(lambda (k+m)
  1352.                   (cons (car k+m)
  1353.                     (convert-methods (cdr k+m)
  1354.                              method-alist wrappers)))
  1355.                   (cddr constant)))
  1356.            (mp (cadr constant)))
  1357.        (ecase (cadr mp)
  1358.          (:simple
  1359.           (car alist))
  1360.          (:assoc
  1361.           alist)
  1362.          (:hash-table
  1363.           (let ((table (make-hash-table :test (if (car mp) 'eq 'eql))))
  1364.         (dolist (k+m alist)
  1365.           (setf (gethash (car k+m) table) (cdr k+m)))
  1366.         table)))))))
  1367.  
  1368. (defun compute-secondary-dispatch-function1 (generic-function net
  1369.                          &optional function-p)
  1370.   (if (eq (car net) 'methods)
  1371.       (get-effective-method-function1 generic-function (cadr net))
  1372.       (let* ((name (generic-function-name generic-function))
  1373.          (arg-info (gf-arg-info generic-function))
  1374.          (metatypes (arg-info-metatypes arg-info))
  1375.          (applyp (arg-info-applyp arg-info))
  1376.          (fmc-arg-info (cons (length metatypes) applyp))
  1377.          (arglist (if function-p
  1378.               (make-dfun-lambda-list metatypes applyp)
  1379.               (make-fast-method-call-lambda-list metatypes applyp))))
  1380.     (multiple-value-bind (cfunction constants)
  1381.         (get-function1 `(lambda ,arglist
  1382.                   ,@(unless function-p
  1383.                   `((declare (ignore .pv-cell.
  1384.                              .next-method-call.))))
  1385.                   #+copy-&rest-arg
  1386.                   ,@(when (and applyp function-p)
  1387.                   `((setq .dfun-rest-arg.
  1388.                       (copy-list .dfun-rest-arg.))))
  1389.                   (let ((emf ,net))
  1390.                     ,(make-emf-call metatypes applyp 'emf)))
  1391.                #'net-test-converter
  1392.                #'net-code-converter
  1393.                #'(lambda (form)
  1394.                    (net-constant-converter form generic-function)))
  1395.       #'(lambda (method-alist wrappers)
  1396.           (let* ((alist (list nil))
  1397.              (alist-tail alist))
  1398.         (dolist (constant constants)
  1399.           (let* ((a (or (dolist (a alist nil)
  1400.                   (when (eq (car a) constant)
  1401.                     (return a)))
  1402.                 (cons constant
  1403.                       (or (convert-table
  1404.                        constant method-alist wrappers)
  1405.                       (convert-methods
  1406.                        constant method-alist wrappers)))))
  1407.              (new (list a)))
  1408.             (setf (cdr alist-tail) new)
  1409.             (setf alist-tail new)))
  1410.         (let ((function (apply cfunction (mapcar #'cdr (cdr alist)))))
  1411.           (if function-p
  1412.               function
  1413.               (make-fast-method-call
  1414.                :function (set-function-name function `(sdfun-method ,name))
  1415.                :arg-info fmc-arg-info)))))))))
  1416.  
  1417. (defvar *show-make-unordered-methods-emf-calls* nil)
  1418.  
  1419. (defun make-unordered-methods-emf (generic-function methods)
  1420.   (when *show-make-unordered-methods-emf-calls*
  1421.     (format t "~&make-unordered-methods-emf ~s~%" 
  1422.         (generic-function-name generic-function)))
  1423.   #'(lambda (&rest args)
  1424.       #+copy-&rest-arg (setq args (copy-list args))
  1425.       (let* ((types (types-from-arguments generic-function args 'eql))
  1426.          (smethods (sort-applicable-methods generic-function methods types))
  1427.          (emf (get-effective-method-function generic-function smethods)))
  1428.     (invoke-emf emf args))))
  1429.  
  1430.  
  1431. ;;;
  1432. ;;; The value returned by compute-discriminating-function is a function
  1433. ;;; object.  It is called a discriminating function because it is called
  1434. ;;; when the generic function is called and its role is to discriminate
  1435. ;;; on the arguments to the generic function and then call appropriate
  1436. ;;; method functions.
  1437. ;;; 
  1438. ;;; A discriminating function can only be called when it is installed as
  1439. ;;; the funcallable instance function of the generic function for which
  1440. ;;; it was computed.
  1441. ;;;
  1442. ;;; More precisely, if compute-discriminating-function is called with an
  1443. ;;; argument <gf1>, and returns a result <df1>, that result must not be
  1444. ;;; passed to apply or funcall directly.  Rather, <df1> must be stored as
  1445. ;;; the funcallable instance function of the same generic function <gf1>
  1446. ;;; (using set-funcallable-instance-function).  Then the generic function
  1447. ;;; can be passed to funcall or apply.
  1448. ;;;
  1449. ;;; An important exception is that methods on this generic function are
  1450. ;;; permitted to return a function which itself ends up calling the value
  1451. ;;; returned by a more specific method.  This kind of `encapsulation' of
  1452. ;;; discriminating function is critical to many uses of the MOP.
  1453. ;;; 
  1454. ;;; As an example, the following canonical case is legal:
  1455. ;;;
  1456. ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
  1457. ;;;     (let ((std (call-next-method)))
  1458. ;;;       #'(lambda (arg)
  1459. ;;;            (print (list 'call-to-gf gf arg))
  1460. ;;;            (funcall std arg))))
  1461. ;;;
  1462. ;;; Because many discriminating functions would like to use a dynamic
  1463. ;;; strategy in which the precise discriminating function changes with
  1464. ;;; time it is important to specify how a discriminating function is
  1465. ;;; permitted itself to change the funcallable instance function of the
  1466. ;;; generic function.
  1467. ;;;
  1468. ;;; Discriminating functions may set the funcallable instance function
  1469. ;;; of the generic function, but the new value must be generated by making
  1470. ;;; a call to COMPUTE-DISCRIMINATING-FUNCTION.  This is to ensure that any
  1471. ;;; more specific methods which may have encapsulated the discriminating
  1472. ;;; function will get a chance to encapsulate the new, inner discriminating
  1473. ;;; function.
  1474. ;;;
  1475. ;;; This implies that if a discriminating function wants to modify itself
  1476. ;;; it should first store some information in the generic function proper,
  1477. ;;; and then call compute-discriminating-function.  The appropriate method
  1478. ;;; on compute-discriminating-function will see the information stored in
  1479. ;;; the generic function and generate a discriminating function accordingly.
  1480. ;;;
  1481. ;;; The following is an example of a discriminating function which modifies
  1482. ;;; itself in accordance with this protocol:
  1483. ;;;
  1484. ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
  1485. ;;;     #'(lambda (arg)
  1486. ;;;         (cond (<some condition>
  1487. ;;;                <store some info in the generic function>
  1488. ;;;                (set-funcallable-instance-function
  1489. ;;;                  gf
  1490. ;;;                  (compute-discriminating-function gf))
  1491. ;;;                (funcall gf arg))
  1492. ;;;               (t
  1493. ;;;                <call-a-method-of-gf>))))
  1494. ;;;
  1495. ;;; Whereas this code would not be legal:
  1496. ;;;
  1497. ;;;   (defmethod compute-discriminating-function ((gf my-generic-function))
  1498. ;;;     #'(lambda (arg)
  1499. ;;;         (cond (<some condition>
  1500. ;;;                (set-funcallable-instance-function
  1501. ;;;                  gf
  1502. ;;;                  #'(lambda (a) ..))
  1503. ;;;                (funcall gf arg))
  1504. ;;;               (t
  1505. ;;;                <call-a-method-of-gf>))))
  1506. ;;;
  1507. ;;; NOTE:  All the examples above assume that all instances of the class
  1508. ;;;        my-generic-function accept only one argument.
  1509. ;;;
  1510. ;;;
  1511. ;;;
  1512. ;;;
  1513. (defun slot-value-using-class-dfun (class object slotd)
  1514.   (declare (ignore class))
  1515.   (function-funcall (slot-definition-reader-function slotd) object))
  1516.  
  1517. (defun setf-slot-value-using-class-dfun (new-value class object slotd)
  1518.   (declare (ignore class))
  1519.   (function-funcall (slot-definition-writer-function slotd) new-value object))
  1520.  
  1521. (defun slot-boundp-using-class-dfun (class object slotd)
  1522.   (declare (ignore class))
  1523.   (function-funcall (slot-definition-boundp-function slotd) object))
  1524.  
  1525. (defmethod compute-discriminating-function ((gf standard-generic-function))
  1526.   (with-slots (dfun-state arg-info) gf
  1527.     (typecase dfun-state
  1528.       (null (let ((name (generic-function-name gf)))
  1529.           (when (eq name 'compute-applicable-methods)
  1530.         (update-all-c-a-m-gf-info gf))
  1531.           (cond ((eq name 'slot-value-using-class)
  1532.              (update-slot-value-gf-info gf 'reader)
  1533.              #'slot-value-using-class-dfun)
  1534.             ((equal name '(setf slot-value-using-class))
  1535.              (update-slot-value-gf-info gf 'writer)
  1536.              #'setf-slot-value-using-class-dfun)
  1537.             ((eq name 'slot-boundp-using-class)
  1538.              (update-slot-value-gf-info gf 'boundp)
  1539.              #'slot-boundp-using-class-dfun)
  1540.             ((gf-precompute-dfun-and-emf-p arg-info)
  1541.              (make-final-dfun gf))
  1542.             (t
  1543.              (make-initial-dfun gf)))))
  1544.       (function dfun-state)
  1545.       (cons (car dfun-state)))))
  1546.  
  1547. (defmethod update-gf-dfun ((class std-class) gf)
  1548.   (let ((*new-class* class)
  1549.     #|| (name (generic-function-name gf)) ||#
  1550.     (arg-info (gf-arg-info gf)))
  1551.     (cond #||
  1552.       ((eq name 'slot-value-using-class)
  1553.        (update-slot-value-gf-info gf 'reader))
  1554.       ((equal name '(setf slot-value-using-class))
  1555.        (update-slot-value-gf-info gf 'writer))
  1556.       ((eq name 'slot-boundp-using-class)
  1557.        (update-slot-value-gf-info gf 'boundp))
  1558.       ||#
  1559.       ((gf-precompute-dfun-and-emf-p arg-info)
  1560.        (multiple-value-bind (dfun cache info)
  1561.            (make-final-dfun-internal gf)
  1562.          (set-dfun gf dfun cache info) ; otherwise cache might get freed twice
  1563.          (update-dfun gf dfun cache info))))))
  1564.  
  1565. ;;;
  1566. ;;;
  1567. ;;;
  1568. (defmethod function-keywords ((method standard-method))
  1569.   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords)
  1570.       (analyze-lambda-list (if (consp method)
  1571.                    (early-method-lambda-list method)
  1572.                    (method-lambda-list method)))
  1573.     (declare (ignore nreq nopt keysp restp))
  1574.     (values keywords allow-other-keys-p)))
  1575.  
  1576. (defun method-ll->generic-function-ll (ll)
  1577.   (multiple-value-bind (nreq nopt keysp restp allow-other-keys-p keywords keyword-parameters)
  1578.       (analyze-lambda-list ll)
  1579.     (declare (ignore nreq nopt keysp restp allow-other-keys-p keywords))
  1580.     (remove-if #'(lambda (s)
  1581.            (or (memq s keyword-parameters)
  1582.                (eq s '&allow-other-keys)))
  1583.            ll)))
  1584.  
  1585.  
  1586. ;;;
  1587. ;;; This is based on the rules of method lambda list congruency defined in
  1588. ;;; the spec.  The lambda list it constructs is the pretty union of the
  1589. ;;; lambda lists of all the methods.  It doesn't take method applicability
  1590. ;;; into account at all yet.
  1591. ;;; 
  1592. (defmethod generic-function-pretty-arglist
  1593.        ((generic-function standard-generic-function))
  1594.   (let ((methods (generic-function-methods generic-function))
  1595.     (arglist ()))      
  1596.     (when methods
  1597.       (multiple-value-bind (required optional rest key allow-other-keys)
  1598.       (method-pretty-arglist (car methods))
  1599.     (dolist (m (cdr methods))
  1600.       (multiple-value-bind (method-key-keywords
  1601.                 method-allow-other-keys
  1602.                 method-key)
  1603.           (function-keywords m)
  1604.         ;; we've modified function-keywords to return what we want as
  1605.         ;;  the third value, no other change here.
  1606.         (declare (ignore method-key-keywords))
  1607.         (setq key (union key method-key))
  1608.         (setq allow-other-keys (or allow-other-keys
  1609.                        method-allow-other-keys))))
  1610.     (when allow-other-keys
  1611.       (setq arglist '(&allow-other-keys)))
  1612.     (when key
  1613.       (setq arglist (nconc (list '&key) key arglist)))
  1614.     (when rest
  1615.       (setq arglist (nconc (list '&rest rest) arglist)))
  1616.     (when optional
  1617.       (setq arglist (nconc (list '&optional) optional arglist)))
  1618.     (nconc required arglist)))))
  1619.   
  1620.  
  1621. (defmethod method-pretty-arglist ((method standard-method))
  1622.   (let ((required ())
  1623.     (optional ())
  1624.     (rest nil)
  1625.     (key ())
  1626.     (allow-other-keys nil)
  1627.     (state 'required)
  1628.     (arglist (method-lambda-list method)))
  1629.     (dolist (arg arglist)
  1630.       (cond ((eq arg '&optional)         (setq state 'optional))
  1631.         ((eq arg '&rest)             (setq state 'rest))
  1632.         ((eq arg '&key)              (setq state 'key))
  1633.         ((eq arg '&allow-other-keys) (setq allow-other-keys 't))
  1634.         ((memq arg lambda-list-keywords))
  1635.         (t
  1636.          (ecase state
  1637.            (required (push arg required))
  1638.            (optional (push arg optional))
  1639.            (key      (push arg key))
  1640.            (rest     (setq rest arg))))))
  1641.     (values (nreverse required)
  1642.         (nreverse optional)
  1643.         rest
  1644.         (nreverse key)
  1645.         allow-other-keys)))
  1646.  
  1647.